using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class ChiTietPhieuThueRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //

		                                                          

		//////////////////////////////////////////////////////////

        public static ChiTietPhieuThueDTO CreateChiTietPhieuThueDTOFromChiTietPhieuThueEF(ChiTietPhieuThue objEf) // EF -> DTO
        {
            ChiTietPhieuThueDTO objDTO = new ChiTietPhieuThueDTO();
            Mapper.CreateMap<ChiTietPhieuThue, ChiTietPhieuThueDTO>();
            objDTO = Mapper.Map<ChiTietPhieuThue, ChiTietPhieuThueDTO>(objEf);
            return objDTO;
        }

        public static ChiTietPhieuThue CreateChiTietPhieuThueEFFromChiTietPhieuThueDTO(ChiTietPhieuThueDTO objDTO)    //DTO -> EF
        {
            ChiTietPhieuThue objEF = new ChiTietPhieuThue();
            Mapper.CreateMap<ChiTietPhieuThueDTO, ChiTietPhieuThue>();
            objEF = Mapper.Map<ChiTietPhieuThueDTO, ChiTietPhieuThue>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.ChiTietPhieuThueList
                   select x.MaChiTietPhieuThue).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.ChiTietPhieuThueList
                        orderby x.MaChiTietPhieuThue descending
                        select x.MaChiTietPhieuThue;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(ChiTietPhieuThueDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                ChiTietPhieuThue objEF = CreateChiTietPhieuThueEFFromChiTietPhieuThueDTO(objDTO);
                context.AddToChiTietPhieuThueList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(ChiTietPhieuThueDTO objDTO)
        {
            try
            {
                ChiTietPhieuThue objEF = CreateChiTietPhieuThueEFFromChiTietPhieuThueDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.ChiTietPhieuThueList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<ChiTietPhieuThueDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.ChiTietPhieuThueList
                        orderby x.MaChiTietPhieuThue
                        select x).Skip(Start - 1).Take(Record);
            List<ChiTietPhieuThueDTO> lst = new List<ChiTietPhieuThueDTO>();
            foreach (var obj in query)
            {
                ChiTietPhieuThueDTO lp = ChiTietPhieuThueRepository.CreateChiTietPhieuThueDTOFromChiTietPhieuThueEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<ChiTietPhieuThueDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<ChiTietPhieuThueDTO> lst = new List<ChiTietPhieuThueDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from ChiTietPhieuThue in context.ChiTietPhieuThueList
                        select ChiTietPhieuThue;
            foreach (var objEf in query)
            {
                ChiTietPhieuThueDTO objDTO = CreateChiTietPhieuThueDTOFromChiTietPhieuThueEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public ChiTietPhieuThueDTO LayTheoMa(int MaChiTietPhieuThue)
        {
            ChiTietPhieuThueDTO objDTO = new ChiTietPhieuThueDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from ChiTietPhieuThue in context.ChiTietPhieuThueList
                            where ChiTietPhieuThue.MaChiTietPhieuThue == MaChiTietPhieuThue
                            select ChiTietPhieuThue;
                ChiTietPhieuThue objEF = query.First();
                objDTO = CreateChiTietPhieuThueDTOFromChiTietPhieuThueEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }

//
//Khoa ngoai PhieuThue
//
        public List<ChiTietPhieuThueDTO> LayDanhSachTheoMaPhieuThue( int MaPhieuThue)
        {
            List<ChiTietPhieuThueDTO> lst = new List<ChiTietPhieuThueDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from ChiTietPhieuThue in context.ChiTietPhieuThueList
                         where ChiTietPhieuThue.MaPhieuThue == MaPhieuThue 
                        select ChiTietPhieuThue;
                 foreach (var objEf in query)
                 {
                      ChiTietPhieuThueDTO objDTO = CreateChiTietPhieuThueDTOFromChiTietPhieuThueEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaPhieuThue( int MaPhieuThue, bool XoaKhoaNgoai=false)
        {
            List<ChiTietPhieuThueDTO> lst = this.LayDanhSachTheoMaPhieuThue( MaPhieuThue);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaChiTietPhieuThue;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }


//
//Khoa ngoai KhachHang
//
        public List<ChiTietPhieuThueDTO> LayDanhSachTheoMaKhachHang( int MaKhachHang)
        {
            List<ChiTietPhieuThueDTO> lst = new List<ChiTietPhieuThueDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from ChiTietPhieuThue in context.ChiTietPhieuThueList
                         where ChiTietPhieuThue.MaKhachHang == MaKhachHang 
                        select ChiTietPhieuThue;
                 foreach (var objEf in query)
                 {
                      ChiTietPhieuThueDTO objDTO = CreateChiTietPhieuThueDTOFromChiTietPhieuThueEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaKhachHang( int MaKhachHang, bool XoaKhoaNgoai=false)
        {
            List<ChiTietPhieuThueDTO> lst = this.LayDanhSachTheoMaKhachHang( MaKhachHang);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaChiTietPhieuThue;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }

        public bool Xoa(int MaChiTietPhieuThue, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.ChiTietPhieuThueList
                            where x.MaChiTietPhieuThue == MaChiTietPhieuThue 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

